package com.bitdf.txing.group.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.common.valid.SaveGroup;
import com.bitdf.txing.common.valid.UpdateGroup;
import com.bitdf.txing.group.entity.GroupCategoryEntity;
import com.bitdf.txing.group.service.GroupCategoryService;
import com.bitdf.txing.group.vo.GroupCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;




/**
 * 
 * 组团分类控制器
 * @author Lizhiwei
 * @email 123@qq.com
 * @date 2022-09-05 16:02:12
 */
@RestController
@RequestMapping("${txing.request.path.group}/category")
public class GroupCategoryController {
    @Autowired
    private GroupCategoryService groupCategoryService;


    /**
     * 获取所有组团分类目录
     * @return
     */
    @RequestMapping("/getCategory")
    @SentinelResource(value = "getCategory",blockHandler = "aaa")
    public R getCategory(){
        List<GroupCategoryVo>  category = groupCategoryService.getCategory();
        return R.ok().put("data", category);
    }


    /**
     * 根据id获取某分类
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		GroupCategoryEntity groupCategory = groupCategoryService.getById(id);
        return R.ok().put("data", groupCategory);
    }

    /**
     * 新增分类
     * @param groupCategory
     * @param result
     * @return
     */
    @SaCheckLogin
    @RequestMapping("/save")
    public R save(@Validated(SaveGroup.class) @RequestBody GroupCategoryEntity groupCategory, BindingResult result){
        // 判断校验是否成功
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            return R.error(TxCodeEnume.COMMON_SUBMIT_DATA_EXCEPTION).put("data", errors);
        }
        // TODO 判断一下parent、level是否合法
		groupCategoryService.save(groupCategory);
        return R.ok();
    }

    /**
     * 修改分类
     */
    @SaCheckLogin
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody GroupCategoryEntity groupCategory, BindingResult result){
        // 判断校验是否成功
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            return R.error(TxCodeEnume.COMMON_SUBMIT_DATA_EXCEPTION).put("data", errors);
        }
        boolean b = groupCategoryService.updateById(groupCategory);
        return R.ok();
    }

    /**
     * 删除分类
     */
    @SaCheckLogin
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		groupCategoryService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
